function [results] = tracker(p)
    num_frames = numel(p.img_files);
    % used for OTB-13 benchmark
    rect_positions = zeros(num_frames, 4);
    pos = p.init_pos;
    rot = p.rot;
    sc = p.sc;
    norm_window_sz = p.norm_window_sz;
    norm_target_sz = p.norm_target_sz;
    target_sz = p.target_sz;
    target_hist = [];
    context_hist = [];
    im = imread([p.img_path p.img_files{1}]);
    for frame = 1:num_frames
        if frame>1
        %TESTING
            im = imread([p.img_path p.img_files{frame}]);
            patch = get_affine_subwindow(im, pos, sc, rot, norm_window_sz);
            xhog = get_hog13_feature(patch, p.response_sz);
            [xcr, likelihood_map] = get_cr_feature(patch, p.response_sz, p.norm_likelihood_sz, target_hist, context_hist);
            xt = cat(3, xhog, 3*xcr);
            xt = bsxfun(@times, p.cos_window, xt);
            xtf = fft2(xt);
            hf = bsxfun(@rdivide, hf_num, sum(hf_den, 3)+p.lambda);
            response_cf = real(ifft2(sum(hf .* xtf, 3)));
            response_cf = crop_response(response_cf,...
                floor_odd(p.norm_delta_sz / p.hog_cell_size));
            response_cf = mexResize(response_cf, p.norm_delta_sz, 'auto');
            response_color = getCenterLikelihood(likelihood_map, norm_target_sz);
            response = 0.3*response_color + 0.7*response_cf;
            [row, col] = find(response == max(response(:)), 1);
            center = p.norm_delta_sz / 2;
            pos = pos + ([row, col] - center)*sc;
            
            %SCALE-ROTATION ESTIMATION
            patchL = get_affine_subwindow(im, pos, 1, rot, floor(sc*p.scale_sz));
            patchL = mexResize(patchL, p.norm_scale_sz);
            patchLp = mpolar(double(patchL),p.mag);
            patchLp = Hog13Feature(single(patchLp));
            %patchLp = fhog(single(patchLp)/255, 4, 9);
            %patchLp(:,:,end) = [];
            [ptx, pty, ~] = phaseCorrelate(modelPatch, patchLp);
            tmp_rot = (pty-1)*pi/floor(size(patchLp, 2)/2);
            tmp_sc = exp( (ptx-1) / p.mag);
            if tmp_sc > 1.4, tmp_sc =1.4;disp("Too Big Scale");end
            if tmp_sc < 0.6, tmp_sc =0.6;disp("Too Small Scale");end   
            if tmp_rot > 1, tmp_rot =0;disp("Too Big Rot");end
            if tmp_rot < -1, tmp_rot =0;disp("Too Small Rot");end
            rot = rot + tmp_rot;
            sc = sc * tmp_sc;
            target_sz = target_sz * tmp_sc;
            
        end
        %TRAINING
        [target_hist, context_hist] = update_histogram_model(im, pos, p.target_sz, p.learning_rate_hist, target_hist, context_hist);
        patch = get_affine_subwindow(im, pos, sc, rot, norm_window_sz);
        xhog = get_hog13_feature(patch, p.response_sz);
        [xcr, ~] = get_cr_feature(patch, p.response_sz, p.norm_likelihood_sz, target_hist, context_hist);
        xt = cat(3, xhog, 3*xcr);
        xt = bsxfun(@times, p.cos_window, xt);
        xtf = fft2(xt);
        new_hf_num = bsxfun(@times, p.yf, conj(xtf));
		new_hf_den = conj(xtf) .* xtf;
        
        %SCALE-ROTATION LEARNING
        patchL = get_affine_subwindow(im, pos, 1, rot, floor(sc*p.scale_sz));
        patchL = mexResize(patchL, p.norm_scale_sz);
        patchLp = mpolar(double(patchL),p.mag);
        patchLp = Hog13Feature(single(patchLp));
        % patchLp = fhog(single(patchLp), 4, 9);
        % patchLp = fhog(single(patchLp)/255, 4, 9);
        % patchLp(:,:,end) = [];
        
        if frame == 1
            hf_num = new_hf_num;
            hf_den = new_hf_den;
            modelPatch = patchLp;
        else
            hf_num = (1 - p.learning_rate_cf) * hf_num + p.learning_rate_cf * new_hf_num;
            hf_den = (1 - p.learning_rate_cf) * hf_den + p.learning_rate_cf * new_hf_den;
            modelPatch = (1 - p.learning_rate_scale) * modelPatch + p.learning_rate_scale * patchLp;
        end
        
        if p.visualization == 1
        rect_position = [pos([2,1]) - target_sz([2,1])/2, target_sz([2,1])];
        T = parameters_to_projective_matrix('SIMILARITY',...
                [1,rot,pos(2),pos(1)]);
        [aff,~]= getLKcorner(T, target_sz);
        aff(:,5)=aff(:,1);        
        %points(frame,:) = [aff(1,1),aff(2,1),aff(1,4),aff(2,4),....
        %                     aff(1,3),aff(2,3),aff(1,2),aff(2,2)];
        if frame == 1,  %first frame, create GUI
            figure('Name',['Tracker - ' p.video_path]);
            im_handle = imshow(uint8(im), 'Border','tight', 'InitialMag', 100 + 100 * (length(im) < 500));
            %rect_handle = rectangle('Position',rect_position, 'EdgeColor','r', 'LineWidth',2);
            text_handle = text(10, 10, int2str(frame));
            line_h = line('XData',[0 0 1 1 0],'YData',[0 1 1 0 0], 'Color','g', 'LineWidth',2);
            set(text_handle, 'color', [0 1 1]);
            figure;
            patch_handle = imshow(patch);
        else
            try  %subsequent frames, update GUI
                set(im_handle, 'CData', im)
                set(patch_handle, 'CData', patch);
                %set(rect_handle, 'Position', rect_position)
                set(text_handle, 'string', int2str(frame));
                set(line_h, 'XData',aff(1,:),'YData', aff(2,:));
            catch
                return
            end
        end        
        drawnow
%         pause
        end
    end
    results = rect_positions;
end

% We want odd regions so that the central pixel can be exact
function y = floor_odd(x)
    y = 2*floor((x-1) / 2) + 1;
end